package baseball.controller;

import baseball.domain.Activity;
import baseball.domain.User;
import baseball.mapper.ActivityMapper;
import baseball.support.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;
import java.util.List;

@RequestMapping("member/activity")
@Controller
public class MemberActivityController extends BaseController{
    @Autowired
    private ActivityMapper activityMapper;
    

    /**
     * 全部列表
     *
     * @param model
     * @return
     */
    @RequestMapping("list")
    private String list(Model model) {
        List<Activity> list = activityMapper.selectAll();
        model.addAttribute("list", list);
        return "member/activity/list";
    }

    /**
     * 到添加页面
     *
     * @return
     */
    @GetMapping("add")
    public String add() {
        return "member/activity/add";
    }

    /**
     * 添加活动
     *
     * @param activity
     * @return
     */
    @PostMapping("add")
    public String add(Activity activity) {
        activity.setAddtime(new Date());
        activityMapper.insertSelective(activity);
        return "redirect:list.do";
    }


    /**
     * 活动详情
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("detail/{id}")
    public String detail(@PathVariable Integer id, Model model) {
        Activity activity = activityMapper.selectByPrimaryKey(id);
        model.addAttribute(activity);
        return "member/activity/detail";
    }

    /**
     * 更新活动
     *
     * @param activity
     * @return
     */
    @RequestMapping("update")
    public String update(Activity activity) {
        activityMapper.updateByPrimaryKeySelective(activity);
        return "redirect:list.do";
    }

    /**
     * 删除活动
     *
     * @param id
     * @return
     */
    @RequestMapping("del/{id}")
    public String del(@PathVariable Integer id) {
        activityMapper.deleteByPrimaryKey(id);
        return refresh();
    }
}
